package routers

import (
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"

	v1 "application/api/v1"
	"application/pkg/app"
	"application/pkg/jwt"
)

func RegisterUserRouter(engine *gin.Engine) {
	user := engine.Group("/API/V1/user")
	{
		//注册
		user.POST("/register", v1.Register).Use(Middle)
		//登录
		user.POST("/login", v1.Login)
		//修改用户信息
		user.POST("/update", v1.UploadUser).Use(Middle)
		//获取用户信息
		user.GET("/message", v1.UserMessage).Use(Middle)
		//注销用户信息
		user.POST("/logout", v1.Logout).Use(Middle)
		//删除用户信息
		user.POST("/del", v1.DelUser).Use(Middle)
	}
}

func Middle(c *gin.Context) {
	appG := app.Gin{C: c}
	token := c.Request.Header.Get("token")
	res := jwt.CheckoutToken(token)
	if res == 0 {
		appG.Response(http.StatusBadRequest, "token错误", "")
		c.Abort()
		return
	}
	c.Request.Form.Set("uid", strconv.Itoa(res))
	c.Next()
}
